﻿<!DOCTYPE html>
<html lang="en">
<head>
	<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
	<script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
	<script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script>	
	<script type="text/javascript" src="../../jqwidgets/jqxgrid.sort.js"></script>		
	<script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>	
	<script type="text/javascript" src="../../scripts/demos.js"></script>
	<script type="text/javascript">
		$(document).ready(function () {
			// prepare the data
			
      
			var source =
			{
				datatype: "json",
				datafields: [
					{ name: 'ShippedDate', type: 'date'},
					{ name: 'ShipName'},
					{ name: 'ShipAddress'},
					{ name: 'ShipCity'},
					{ name: 'ShipCountry'}
				],
				url: 'serversorting_data.php',
				sort: function()
				{
					// update the grid and send a request to the server.
					$("#jqxgrid").jqxGrid('updatebounddata', 'sort');
				}
			};	
				
			var dataAdapter = new $.jqx.dataAdapter(source);
	
			// initialize jqxGrid
			$("#jqxgrid").jqxGrid(
			{		
				source: dataAdapter,
				
				sortable: true,
				columns: [
					{ text: 'Shipped Date', datafield: 'ShippedDate', cellsformat: 'd', width: 100 },
					{ text: 'Ship Name', datafield: 'ShipName', width: 200 },
					{ text: 'Address', datafield: 'ShipAddress', width: 180 },
					{ text: 'City', datafield: 'ShipCity', width: 100 },
					{ text: 'Country', datafield: 'ShipCountry', width: 140 }
				]
			});
		});
		</script>
	</head>
	<body class='default'>
	<div id="jqxgrid"></div>
	</body>
</html>
<!--serversorting_data.php
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
	print "can't find $database";
}
	
$query = "SELECT * FROM Orders";
// sort data.
if (isset($_GET['sortdatafield']))
{
	$sortfield = $_GET['sortdatafield'];
	$sortorder = $_GET['sortorder'];
		
	if ($sortfield != NULL)
	{
		if ($sortorder == "desc")
		{
			$query = "SELECT * FROM Orders ORDER BY" . " " . $sortfield . " DESC";
		}
		else if ($sortorder == "asc")
		{
			$query = "SELECT * FROM Orders ORDER BY" . " " . $sortfield . " ASC";
		}			
	}
}

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());

// get data and store in a json array
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$orders[] = array(
		'OrderDate' => $row['OrderDate'],
		'ShippedDate' => $row['ShippedDate'],
		'ShipName' => $row['ShipName'],
		'ShipAddress' => $row['ShipAddress'],
		'ShipCity' => $row['ShipCity'],
		'ShipCountry' => $row['ShipCountry']
		);
}
  
echo json_encode($orders);
-->